<?php
/**
 * Class that operate on table 'competencia'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-11-23 21:12
 */
class CompetenciaMySqlDAO implements CompetenciaDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return CompetenciaMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM competencia WHERE id_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM competencia';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM competencia ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param competencia primary key
 	 */
	public function delete($id_competencia){
		$sql = 'DELETE FROM competencia WHERE id_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id_competencia);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param CompetenciaMySql competencia
 	 */
	public function insert($competencia){
		$sql = 'INSERT INTO competencia (nombre_competencia, modalidad, fecha_competencia, hora_competencia, id_deporte, id_espacio, descripcion_competencia, estado_competencia) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($competencia->nombreCompetencia);
		$sqlQuery->set($competencia->modalidad);
		$sqlQuery->set($competencia->fechaCompetencia);
		$sqlQuery->set($competencia->horaCompetencia);
		$sqlQuery->setNumber($competencia->idDeporte);
		$sqlQuery->setNumber($competencia->idEspacio);
		$sqlQuery->set($competencia->descripcionCompetencia);
		$sqlQuery->set($competencia->estadoCompetencia);

		$id = $this->executeInsert($sqlQuery);	
		$competencia->idCompetencia = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param CompetenciaMySql competencia
 	 */
	public function update($competencia){
		$sql = 'UPDATE competencia SET nombre_competencia = ?, modalidad = ?, fecha_competencia = ?, hora_competencia = ?, id_deporte = ?, id_espacio = ?, descripcion_competencia = ?, estado_competencia = ? WHERE id_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($competencia->nombreCompetencia);
		$sqlQuery->set($competencia->modalidad);
		$sqlQuery->set($competencia->fechaCompetencia);
		$sqlQuery->set($competencia->horaCompetencia);
		$sqlQuery->setNumber($competencia->idDeporte);
		$sqlQuery->setNumber($competencia->idEspacio);
		$sqlQuery->set($competencia->descripcionCompetencia);
		$sqlQuery->set($competencia->estadoCompetencia);

		$sqlQuery->setNumber($competencia->idCompetencia);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM competencia';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByNombreCompetencia($value){
		$sql = 'SELECT * FROM competencia WHERE nombre_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByModalidad($value){
		$sql = 'SELECT * FROM competencia WHERE modalidad = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFechaCompetencia($value){
		$sql = 'SELECT * FROM competencia WHERE fecha_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByHoraCompetencia($value){
		$sql = 'SELECT * FROM competencia WHERE hora_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdDeporte($value){
		$sql = 'SELECT * FROM competencia WHERE id_deporte = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdEspacio($value){
		$sql = 'SELECT * FROM competencia WHERE id_espacio = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescripcionCompetencia($value){
		$sql = 'SELECT * FROM competencia WHERE descripcion_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEstadoCompetencia($value){
		$sql = 'SELECT * FROM competencia WHERE estado_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByNombreCompetencia($value){
		$sql = 'DELETE FROM competencia WHERE nombre_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByModalidad($value){
		$sql = 'DELETE FROM competencia WHERE modalidad = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFechaCompetencia($value){
		$sql = 'DELETE FROM competencia WHERE fecha_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByHoraCompetencia($value){
		$sql = 'DELETE FROM competencia WHERE hora_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdDeporte($value){
		$sql = 'DELETE FROM competencia WHERE id_deporte = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdEspacio($value){
		$sql = 'DELETE FROM competencia WHERE id_espacio = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescripcionCompetencia($value){
		$sql = 'DELETE FROM competencia WHERE descripcion_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEstadoCompetencia($value){
		$sql = 'DELETE FROM competencia WHERE estado_competencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return CompetenciaMySql 
	 */
	protected function readRow($row){
		$competencia = new Competencia();
		
		$competencia->idCompetencia = $row['id_competencia'];
		$competencia->nombreCompetencia = $row['nombre_competencia'];
		$competencia->modalidad = $row['modalidad'];
		$competencia->fechaCompetencia = $row['fecha_competencia'];
		$competencia->horaCompetencia = $row['hora_competencia'];
		$competencia->idDeporte = $row['id_deporte'];
		$competencia->idEspacio = $row['id_espacio'];
		$competencia->descripcionCompetencia = $row['descripcion_competencia'];
		$competencia->estadoCompetencia = $row['estado_competencia'];

		return $competencia;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return CompetenciaMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>